package util

// 次消息转发都是为了topic miss spell
import (
	"bgs/db"
	"github.com/nsqio/go-nsq"
	"go.uber.org/zap"
)

// NsqForwarder 将 一个topic转发到另一个topic
type NsqForwarder struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	producer  *nsq.Producer
	topicFrom string
	topicTo   string
}

// NewNsqForwarder create a new NsqForwarder
func NewNsqForwarder(
	pgdao *db.PgDao,
	slog *zap.SugaredLogger,
	producer *nsq.Producer,
	topicFrom string,
	topicTo string,
) *NsqForwarder {
	return &NsqForwarder{
		dao:       pgdao,
		slog:      slog.With("module", "NsqForwarder"),
		producer:  producer,
		topicFrom: topicFrom,
		topicTo:   topicTo,
	}
}

func (h *NsqForwarder) HandleMessage(m *nsq.Message) error {
	h.slog.Infof("NsqForwarder handler message=>%+v ,%s", m.ID, string(m.Body))
	err := h.producer.Publish(h.topicTo, m.Body)
	if err != nil {
		h.slog.Errorf("NsqForwarder forward msg err:%s", err.Error())
		return err
	}
	return nil
}
